package advice;


import entity.ExceptionResult;
import entity.Result;
import enums.ExceptionEnum;
import exception.MyException;
import exception.AllException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class CommonExceptionHandler {
    @ExceptionHandler(MyException.class)
    @ResponseBody
    public ResponseEntity<ExceptionResult> handleException(MyException e){
        ExceptionEnum em = e.getExceptionEnum();
        System.out.println(em.getMessage());
        return  ResponseEntity.status(em.getCode()).body(new ExceptionResult(em));
    }

    @ExceptionHandler(AllException.class)
    @ResponseBody
    public Map<String,Object> handleNovalueException(AllException e){
        Map<String,Object>map=new HashMap<>();
        ExceptionEnum em = e.getExceptionEnum();
        map.put("flag",false);
        map.put("code",em.getCode());
        map.put("message",em.getMessage());
        e.printStackTrace();
        return  map;
    }
}
